package com.wangsd.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LuckyMachineUtils {

    public static String runLuckyMachine(List<String> ids, List<Double> probablity) {

        List<Double> realprobablity = new ArrayList<>(probablity.size() + 1);
        realprobablity.add(0, 0.0);
        for (int i = 1; i < (probablity.size() + 1); i++) {
            double d = realprobablity.get(i - 1) + probablity.get(i - 1);
            realprobablity.add(i, d);
        }
        double k = Math.random();
        String res = "" ;
        for (int i = 0; i < realprobablity.size() - 1; i++) {
            double vp = realprobablity.get(i);
            double vn = realprobablity.get(i + 1);
            if (vp <= k && k < vn) {
                res = ids.get(i);
                break;
            }
        }
        return res;
    }

    /*获取到配置*/
    public static String run(List<String> ids, List<Double> probablity) {
        String tmp = "";
        for (int i = 0; i < ids.size(); i++) {
            tmp = runLuckyMachine(ids, probablity);
            if (ids.get(i) == tmp) {
                break;
            } else {
                continue;
            }
        }
        return tmp;
    }

    public static void main(String[] args) {
        /*List<String> ids = Arrays.asList(1, 2L, 3L, 100L);
        List<Double> probablity = Arrays.asList(0.8, 0.002, 0.05, 0.6);
        Long id = run(ids, probablity);
        System.out.println("获取到商品的id:" + id);*/
        /*String result = "";
        Long start = System.currentTimeMillis();
        for (int i = 0; i <300 ; i++) {
            result += "xxx";
        }
        System.out.println(System.currentTimeMillis()-start);

        Long start1 = System.currentTimeMillis();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 300 ; i++) {
            stringBuilder.append("xxx");
        }
        System.out.println(System.currentTimeMillis()-start1);*/
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("1");
        list.add("1");
        list.add(null);
        String join = String.join("-", list);
        System.out.println(join);

        String phone = "18980954310";
        phone.substring(phone.length()-4,4);

        String join1 = StringUtils.join(list, ",");
        System.out.println(join1);
    }
}